package com.example.service;

import com.example.constant.MessageTypeConstant;
import com.example.entity.Attachment;
import com.example.entity.ChargingPile;
import com.example.entity.body.impl.ASDUBody;
import com.example.entity.factory.Unpooled;
import com.example.entity.message.Message;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.AttributeKey;

/**
 * 远程充电
 * 
 * @author yao
 *
 */
public class RemoteElectricHandler extends ChannelInboundHandlerAdapter {

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		// TODO Auto-generated method stub

		Message message = (Message) msg;

		ASDUBody body = message.getBody();
		Byte type = body.getType();

		if (type == MessageTypeConstant.REMOTE_ELECTRIC_MESSAGE_CS) {
			System.out.println("调用了 RemoteElectricHandler");
			Attachment[] attachments = body.getAttachments();
			Attachment attachment = attachments[0];
			byte[] bs = attachment.getValue();
			System.out.println("记录号: " + bs[3]);
			if (bs[3] == 0xd) {
				System.out.println("remote 1");
				byte b = bs[30];
				// 计费id 交易流水
				if (b == 1) {
					System.out.println("操作成功");
				} else {
					System.out.println("操作失败");
				}
			} else if (bs[3] == 0x2) {
				// 开始充电时间，结束充电时间
				System.out.println("remote 2");
				String id = (String) ctx.channel().attr(AttributeKey.valueOf("id")).get();
				ctx.writeAndFlush(Unpooled.wrapperRemoteElectricAffirmMessage(id));
			}

		}
	}

}
